package org.example.realtime.jtp.dim.function;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.flink.api.common.functions.RichFilterFunction;
import org.apache.flink.configuration.Configuration;

import java.util.HashSet;
import java.util.Set;

/**
 * @Title: DimDataFilterFunction
 * @Author Lianzy
 * @Package org.example.realtime.jtp.dim.function
 * @Date 2025/6/2 21:04
 * @description 依据指定维度表名称，从topic-db中过滤出维度表CDC操作数据，并且操作类型为插入、更新和删除。
 */
public class DimDataFilterFunction extends RichFilterFunction<String> {
    //维度数据表名称
    private  Set<String> dimSet  ;

    //数据操作类型
    private final Set<String> operateTypeSet = new HashSet<String>(){{
        add("insert"); add("update"); add("delete");
    }};

    public DimDataFilterFunction(Set<String> dimSet) {
        this.dimSet = dimSet ;
    }

    @Override
    public void open(Configuration parameters) throws Exception {
        super.open(parameters);
    }

    @Override
    public boolean filter(String value) throws Exception {
        /*
          {
             "operate_type": "insert",
             "db_name": "jtp_mall",
             "operate_data": {
                "dic_code": "1306",
                "create_time": "2025-01-11 11:30:49.0",
                "parent_code": "13",
                "dic_name": "不想买了",
                "operate_time": "2025-01-11 11:30:49.0"
             },
             "table_name": "base_dic",
             "ts_ms": 1737688970209
          }
        */
        JSONObject jsonObject = JSON.parseObject(value);
        //获取表名称
        String tableName = jsonObject.getString("table_name");
        //操作类型
        String operateType = jsonObject.getString("operate_type");
        //判断过滤
        return dimSet.contains(tableName) && operateTypeSet.contains(operateType);
    }

    @Override
    public void close() throws Exception {
        super.close();
    }
}
